home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / jamendo / JamendoSaxHandler.py < prev    next >
Encoding:
Python Source  |  2009-04-07  |  3.3 KB  |  128 lines

  1. # -*- coding: utf-8 -*-
  2.  
  3. # JamendoSaxHandler.py
  4. #
  5. # Copyright (C) 2007 - Guillaume Desmottes
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2, or (at your option)
  10. # any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20.  
  21. import xml.sax, xml.sax.handler
  22.  
  23. markups = ["JamendoData", "Artists", "artist", "Albums", "album", "Tracks", "track"]
  24. ignore = ["location", "country", "state", "city", "latitude", "longitude"]
  25.  
  26. class JamendoSaxHandler(xml.sax.handler.ContentHandler):
  27.     def __init__(self):
  28.         xml.sax.handler.ContentHandler.__init__(self)
  29.  
  30.         self.current = {}
  31.  
  32.     def startElement(self, name, attrs):
  33.         self.__text = ""
  34.         self.__ignore = False
  35.  
  36.         if name in markups:
  37.             fct = getattr (self, "start" + name)
  38.             fct (attrs)
  39.  
  40.         if name in ignore:
  41.             self.__ignore = True
  42.  
  43.     def endElement(self, name):
  44.         if name in markups:
  45.             fct = getattr (self, "end" + name)
  46.             fct ()
  47.         elif self.__ignore is False:
  48.             self.current[name] = self.__text
  49.  
  50.     def characters(self, content):
  51.         if self.__ignore is False:
  52.             self.__text = self.__text + content
  53.  
  54.     # start markups
  55.     def startJamendoData (self, attrs):
  56.         pass
  57.  
  58.     def startArtists (self, attrs):
  59.         self.artists = {}
  60.  
  61.     def startartist (self, attrs):
  62.         self.artist = {}
  63.         for attr in attrs.getNames():
  64.             self.artist[attr] = attrs[attr]
  65.         self.current = self.artist
  66.  
  67.     def startAlbums (self, attrs):
  68.         self.albums = {}
  69.  
  70.     def startalbum (self, attrs):
  71.         self.album = {}
  72.         for attr in attrs.getNames():
  73.             self.album[attr] = attrs[attr]
  74.         self.current = self.album
  75.  
  76.     def startTracks (self, attrs):
  77.         self.tracks = {}
  78.  
  79.     def starttrack (self, attrs):
  80.         self.track = {}
  81.         for attr in attrs.getNames():
  82.             self.track[attr] = attrs[attr]
  83.         self.current = self.track
  84.  
  85.     # end markups
  86.     def endJamendoData (self):
  87.         pass # end of file
  88.  
  89.     def endArtists (self):
  90.         pass # we have load all artists
  91.  
  92.     def endartist (self):
  93.         self.artists[self.artist['id']] = self.artist
  94.         
  95.     def endAlbums (self):
  96.         self.artist['ALBUMS'] = self.albums
  97.  
  98.     def endalbum (self):
  99.         self.albums[self.album['id']] = self.album
  100.  
  101.     def endTracks (self):
  102.         self.album['TRACKS'] = self.tracks
  103.  
  104.     def endtrack (self):
  105.         self.tracks[self.track['id']] = self.track
  106.  
  107.  
  108. if __name__ == "__main__":
  109.     parser = xml.sax.make_parser()
  110.     handler = JamendoSaxHandler()
  111.     parser.setContentHandler(handler)
  112.     datasource = open("/tmp/dbdump.en.xml")
  113.     #datasource = open("exemple_jamendo.xml")
  114.     parser.parse(datasource)
  115.     #print handler.artists
  116.     #print handler.albums
  117.     #print handler.tracks
  118.  
  119.     tracks = handler.tracks
  120.     artists = handler.artists
  121.     albums = handler.albums
  122.     for track_key in tracks.keys():
  123.         track = tracks[track_key]
  124.         album = albums[track['albumID']]
  125.         artist = artists[album['artistID']]
  126.         #print track['dispname'], track['trackno'], track['lengths'], album['dispname'], artist['dispname']
  127.         print album['P2PLinks']
  128.